<?php
/**
 * User: 1971229701@qq.com
 * Date: 2020/11/3/003
 * Time: 13:30
 */

namespace app\admin\controller;


use app\admin\logic\BannerLogic;
use think\Request;
use think\Db;
class Banner extends BaseAdmin
{
    public function listing()
    {
        return $this->fetch();
    }

    public function ajax()
    {
        $limit = $this->_get('limit', 10);
        $r = (new BannerLogic())->listing($limit);
        return $r;
    }


    public function editsort()
    {
        $state = array();
        $id = $_POST['id'];
        $des = $_POST['des'];
        $res = Db::table('index_banner')->where(array('id'=>$id))->update(array('sort'=>$des));
        if( $res ) {
            return 1;
        }else{
            return 2;
        }
    }

    public function create(Request $request)
    {
        if ($request->isPost()) {
            $data = $request->only(['click_url','img']);
            $logic = new BannerLogic();
            if ($logic->create($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {
            return $this->fetch();
        }
    }

    public function update(Request $request)
    {

        $logic = new BannerLogic();
        if ($request->isPost()) {
            $data = $request->only(['click_url','img','id']);
            if ($logic->update($data) === false) {
                return apiReturnFail($logic->getError());
            }
            return apiReturnSuc();
        } else {

            $id = $this->_get('id');
            $r = $logic->update($id, 'get');
            $this->assign('info', $r);
            return $this->fetch();
        }
    }

    public function delete($id)
    {
        $r = (new BannerLogic())->delete($id);
        return apiReturnSuc($r);
    }
}